"""
实体组件系统(ECS)核心实现
"""
from dataclasses import dataclass
from typing import Dict, Type, List, Set

@dataclass
class Component:
    """组件基类"""
    pass

class Entity:
    """游戏实体类"""
    def __init__(self, entity_id=None):
        self.id = entity_id
        self.components: Dict[Type[Component], Component] = {}
        self.tags: Set[str] = set()
    
    def add_component(self, component: Component):
        """添加组件"""
        self.components[type(component)] = component
        return self
    
    def get_component(self, component_type: Type[Component]) -> Component:
        """获取组件"""
        return self.components.get(component_type)
    
    def has_component(self, component_type: Type[Component]) -> bool:
        """检查是否有指定组件"""
        return component_type in self.components
    
    def add_tag(self, tag: str):
        """添加标签"""
        self.tags.add(tag)
        return self
    
    def has_tag(self, tag: str) -> bool:
        """检查是否有指定标签"""
        return tag in self.tags

class System:
    """系统基类"""
    def update(self, entities: List[Entity], delta_time: float):
        """更新系统"""
        pass

class World:
    """游戏世界管理所有实体和系统"""
    def __init__(self):
        self.entities: Dict[int, Entity] = {}
        self.systems: List[System] = []
        self.next_entity_id = 0
    
    def create_entity(self) -> Entity:
        """创建新实体"""
        entity = Entity(self.next_entity_id)
        self.entities[self.next_entity_id] = entity
        self.next_entity_id += 1
        return entity
    
    def add_system(self, system: System):
        """添加系统"""
        self.systems.append(system)
    
    def update(self, delta_time: float):
        """更新所有系统"""
        for system in self.systems:
            system.update(list(self.entities.values()), delta_time)